﻿using System;
using System.Xml.Linq;

namespace EHR.Ctrls.QueryDesign
{
    public class ColInfo : IXmlItem
    {
        private FieldType _fieldType = FieldType.String;
        public string FieldName { get; set; }
        public string DisplayName { get; set; }
        public int Width { get; set; }
        public bool IsVisible { get; set; }
        public bool AllowNull { get; set; }
        public string StringFormat { get; set; }

        public ColInfo()
        {
            IsVisible = true;
        }

        public FieldType FieldType 
        {
            get { return _fieldType; }
            set 
            { 
                _fieldType = value;
                if (_fieldType == FieldType.DateTime && StringFormat == "")
                {
                    StringFormat = "yyyy-MM-dd";
                }
            }
        }

        public XElement ToXml()
        {
            XElement el = new XElement("Field", 
                new XAttribute("FieldName", FieldName),
                new XAttribute("DisplayName", DisplayName),
                new XAttribute("FieldType", (int)FieldType),
                new XAttribute("StringFormat", StringFormat),
                new XAttribute("AllowNull", AllowNull),
                new XAttribute("IsVisible", IsVisible),
                new XAttribute("Width", Width));

            return el;
        }
        
        public void FromXml(XElement el)
        {
            FieldName = el.Attribute("FieldName").Value;
            DisplayName = el.Attribute("DisplayName").Value;
            //先StringFormat后FieldType
            StringFormat = el.Attribute("StringFormat") == null ? "" : el.Attribute("StringFormat").Value;
            FieldType = (FieldType)int.Parse(el.Attribute("FieldType").Value);
            AllowNull = el.Attribute("AllowNull") == null ? false : bool.Parse(el.Attribute("AllowNull").Value);
            IsVisible = bool.Parse(el.Attribute("IsVisible").Value);
            Width = int.Parse(el.Attribute("Width").Value);
        }

        public Type FieldDotNetType
        {
            get
            {
                switch (FieldType)
                {
                    case FieldType.String:
                        return typeof(string);
                    case FieldType.Int:
                        return AllowNull ? typeof(int?) : typeof(int);
                    case FieldType.Float:
                        return AllowNull ? typeof(double?) : typeof(double);
                    case FieldType.DateTime:
                        return AllowNull ? typeof(DateTime?) : typeof(DateTime);
                    case FieldType.Bool:
                        return AllowNull ? typeof(bool?) : typeof(bool);
                    default:
                        return typeof(object);
                }
            }
        }
    }

    public enum FieldType
    {
        String,
        Bool,
        Int,
        Float,
        DateTime
    }
}
